package com.niiwoo.civet.trade.service.local.contract;

import com.niiwoo.civet.trade.enums.FadadaContractTemplateEnum;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.niiwoo.civet.trade.dao.entity.ProjectContract;
import com.niiwoo.civet.trade.dao.mapper.ProjectContractMapperExt;
import com.niiwoo.civet.trade.dto.request.ProjectContractInfoRequestDTO;
import com.niiwoo.civet.trade.dto.response.contract.ProjectContractResponseDTO;
import com.niiwoo.civet.trade.enums.FadadaContractTemplateEnum.TemplateTypeEnum;

@Service
public class ProjectContractLocalService {

	@Autowired
	private ProjectContractMapperExt projectContractMapperExt;

	/**
	 * 查询用户在指定标的签署的指定类型合同
	 * 
	 * @param userId
	 * @param projectId
	 * @param templateType
	 * @return
	 */
	public ProjectContractResponseDTO queryProjectContract(String userId, Long projectId, TemplateTypeEnum templateType) {
		ProjectContractResponseDTO result = new ProjectContractResponseDTO();
		Assert.notNull(userId, "数据不可空");
		Assert.notNull(projectId, "用户ID不可空");
		Assert.notNull(templateType, "标的ID不可空");
		ProjectContract projectContract = projectContractMapperExt
				.queryProjectContractByUserIdAndProjectIdAndContractType(userId, projectId, templateType.getType());
		if (projectContract == null) {
			return null;
		}
		BeanUtils.copyProperties(projectContract, result);
		return result;
	}

	public ProjectContractResponseDTO queryProjectContractByProjectIdAndContractType(Long projectId, TemplateTypeEnum templateType) {
		ProjectContractResponseDTO result = new ProjectContractResponseDTO();
		Assert.notNull(projectId, "用户ID不可空");
		Assert.notNull(templateType, "标的ID不可空");
		ProjectContract projectContract = projectContractMapperExt
				.queryProjectContractByProjectIdAndContractType(projectId, templateType.getType());
		if (projectContract == null) {
			return null;
		}
		BeanUtils.copyProperties(projectContract, result);
		return result;
	}
	
	public List<ProjectContractResponseDTO> queryProjectContracts(ProjectContractInfoRequestDTO requestDTO) {
		List<Integer> contractType = requestDTO.getContractTypes();
		Long projectId = requestDTO.getProjectId();
		Assert.notEmpty(contractType, "合同类型不可空");
		Assert.notNull(projectId, "标的ID不可空");
		List<ProjectContractResponseDTO> responseDTOs = new ArrayList<>();
		for (int i = 0; i < contractType.size(); i++) {
			ProjectContract projectContract = projectContractMapperExt
					.queryProjectContractByProjectIdAndContractType(projectId, contractType.get(i).byteValue());
			if (Objects.isNull(projectContract)||StringUtils.isBlank(projectContract.getDownloadUrl())||StringUtils.isBlank(projectContract.getViewUrl()) ) {
				continue;
			}
			ProjectContractResponseDTO result = new ProjectContractResponseDTO();
			BeanUtils.copyProperties(projectContract, result);
			responseDTOs.add(result);
		}
		 
		return responseDTOs;
	}
}
